import { auditPage, invoiceReject } from '../services/dispatch';
import { invoicePage, checkInvoice, invoice } from '../services/service';
import { audit, income, pay } from '../services/operate';
import React from 'react';
import { Input, InputNumber, message, Select, Radio } from 'antd';

const { Option } = Select;

const { TextArea } = Input;

/**
 * 客户数据模型
 */

//表格列表信息
const columns = [
  {
    title: 'load号',
    dataIndex: 'loadNo',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '订单状态',
    dataIndex: 'status',
    width: '80px',
    editable: false,
    ellipsis: true,
    render: text => {
      switch (text) {
        case 'ENTRY':
          return <span>生成</span>;
        case 'SPLIT':
          return <span>拆单</span>;
        case 'SPLITED':
          return <span>拆单完成</span>;
        case 'DISPATCH':
          return <span>调度</span>;
        case 'COST_CONFIRMATION':
          return <span>成本确认</span>;
        case 'COST_AUDIT':
          return <span>成本审核</span>;
        case 'ORDER_AUDIT':
          return <span>订单审核</span>;
        case 'COMMON':
          return <span>承运</span>;
        case 'DELIVERY':
          return <span>送达</span>;
        case 'GATHERING':
          return <span>待收款</span>;
        case 'GATHERING_AUDIT':
          return <span>收入审核</span>;
        case 'INVOICE':
          return <span>开票</span>;
        case 'FINISH':
          return <span>完成</span>;
        default:
          return <span />;
      }
    },
  },
  {
    title: '业务类型',
    dataIndex: 'busType',
    width: '80px',
    editable: true,
    ellipsis: true,
    render: text => {
      switch (text) {
        case 'LD':
          return <span>零担</span>;
        case 'DC':
          return <span>单程</span>;
        case 'WF':
          return <span>往返</span>;
        default:
          return <span />;
      }
    },
  },
  {
    title: '要车类型',
    dataIndex: 'carType',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '提货日期',
    dataIndex: 'pickDate',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '到货日期',
    dataIndex: 'aogDate',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '装货工厂',
    dataIndex: 'loadingFactory',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '装货地',
    dataIndex: 'loadingPlace',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '卸货工厂',
    dataIndex: 'loadoffFactory',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '卸货地',
    dataIndex: 'loadoffPlace',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '数量',
    dataIndex: 'amount',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '体积',
    dataIndex: 'volume',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '重量',
    dataIndex: 'weight',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '提货费',
    dataIndex: 'pickFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '干线费',
    dataIndex: 'arteryFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '送货费',
    dataIndex: 'dropoffFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '加点费',
    dataIndex: 'workoverFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '装货费',
    dataIndex: 'loadupFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '卸货费',
    dataIndex: 'loadoffFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '包车费',
    dataIndex: 'carFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '燃油补助费',
    dataIndex: 'fuelFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '加班费',
    dataIndex: 'callbackFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '押车费',
    dataIndex: 'escortinFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '小费',
    dataIndex: 'tipFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '短驳费',
    dataIndex: 'shuttleFee',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '总费用',
    dataIndex: 'feeSum',
    width: '80px',
    editable: true,
    ellipsis: true,
  },
];

// 搜索框字段
const searchFields = [
  {
    cols: [
      {
        md: 6,
        label: '线路',
        name: 'line',
        component: <Input placeholder="请输入线路" />,
      },
      {
        md: 6,
        label: 'LOAD号',
        name: 'loadNo',
        component: <Input placeholder="请输入LOAD号" />,
      },
      {
        md: 6,
        label: '订单状态',
        name: 'status',
        component: (
          <Select placeholder="请输入订单状态">
            <Option value="SPLIT">拆单</Option>
            <Option value="SPLITED">拆单完成</Option>
            <Option value="DISPATCH">调度</Option>
            <Option value="COST_AUDIT">成本审核</Option>
            <Option value="ORDER_AUDIT">订单审核</Option>
          </Select>
        ),
      },
    ],
  },
];

// 填写实际收入表单列
const moneyRows = [
  {
    cols: [
      {
        label: '实际收入',
        name: 'realIncome',
        md: 12,
        rules: [{ required: true, message: '请输入实际收入！' }],
        component: <InputNumber placeholder="请输入实际收入" />,
      },
    ],
  },
];

// 开票表单
const invoiceRows = [
  {
    cols: [
      {
        label: '订单金额',
        name: 'orderAmount',
        md: 12,
        rules: [{ required: true, message: '请输入订单金额！' }],
        component: <InputNumber placeholder="请输入订单金额" />,
      },
      {
        label: '发票金额',
        name: 'invoiceAmount',
        md: 12,
        rules: [{ required: true, message: '请输入发票金额！' }],
        component: <InputNumber placeholder="请输入发票金额" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '发票号',
        name: 'invoiceNo',
        md: 12,
        rules: [{ required: true, message: '请输入发票号！' }],
        component: <Input placeholder="请输入发票号" />,
      },
      {
        label: '是否开全',
        name: 'isFull',
        md: 12,
        rules: [{ required: true, message: '必选' }],
        component: (
          <Radio.Group defaultValue="INVOICE">
            <Radio.Button value="INVOICE">足额</Radio.Button>
            <Radio.Button value="INVOICE_HALF">非足额</Radio.Button>
          </Radio.Group>
        ),
      },
    ],
  },
  {
    cols: [
      {
        label: '开票资料',
        name: 'invoiceInfo',
        md: 24,
        rules: [{ required: true, message: '请输入开票资料！' }],
        component: <TextArea rows={4} readOnly={true} />,
      },
    ],
  },
];

export default {
  namespace: 'auditOrder',

  state: {
    searchFields: searchFields, // 搜索框信息
    searchValues: {
      status: {
        name: 'status',
        value: 'ORDER_AUDIT',
        touched: true,
        dirty: false,
      },
    }, // 搜索框里的值
    pageList: [], // 表格数据
    invoicePageList: [], // 开票数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    }, // 表格分布数据
    columns: columns, // 表格列信息
    selectionIds: [], // 表格选择ID
    selectedRows: [], // 表格选择行
    moneyModal: false, // 申请费用弹窗
    modalForm: {}, // 申请费用表单数据
    moneyRows: moneyRows, // 是请费用表单行
    invoiceModal: false, // 开票弹窗
    invoiceForm: {}, // 申请费用表单数据
    invoiceRows: invoiceRows, // 是请费用表单行
  },

  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(auditPage, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *invoicePage({ payload }, { call, put }) {
      const res = yield call(invoicePage, payload);
      yield put({
        type: 'invoicePage_r',
        payload: res,
      });
    },
    *audit({ payload }, { call, put }) {
      const res = yield call(audit, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      });
    },
    *income({ payload }, { call, put }) {
      const res = yield call(income, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      });
    },
    *invoice({ payload }, { call, put }) {
      const res = yield call(invoice, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      });
    },
    *pay({ payload }, { call, put }) {
      const res = yield call(pay, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      });
    },
    *checkInvoice({ payload }, { call, put }) {
      const res = yield call(checkInvoice, payload);
      yield put({
        type: 'check_r',
        payload: res,
      });
    },
    *invoiceReject({ payload }, { call, put }) {
      const res = yield call(invoiceReject, payload);
      yield put({
        type: 'audit_r',
        payload: res,
      })
    }
  },
  reducers: {
    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },

    // 改变分页插件
    handlePagination(state, action) {
      const { payload } = action;
      const { pagination } = state;
      let newPagination = {
        ...pagination,
        ...payload,
      };
      return {
        ...state,
        pagination: newPagination,
      };
    },
    // 表格选中Id
    handleSelection(state, action) {
      const { selectedRowKeys, selectedRows } = action.payload;
      return {
        ...state,
        selectionIds: selectedRowKeys,
        selectedRows: selectedRows,
      };
    },
    // 费用表单值绑定
    handleModalChange(state, action) {
      const { modalForm } = state;
      const { fields } = action.payload;
      const newModalForm = {
        ...modalForm,
        ...fields,
      };
      return {
        ...state,
        modalForm: newModalForm,
      };
    },

    handleInvoiceChange(state, action) {
      const { invoiceForm } = state;
      const { fields } = action.payload;
      const newModalForm = {
        ...invoiceForm,
        ...fields,
      };
      return {
        ...state,
        invoiceForm: newModalForm,
      };
    },

    handleInvoiceModal(state, action) {
      return {
        ...state,
        invoiceModal: false,
      };
    },

    // 处理申请费用
    handleMoneyModal(state, action) {
      const { moneyModal } = state;
      return {
        ...state,
        moneyModal: !moneyModal,
        modalForm: {},
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
          selectionIds: [],
          selectedRows: [],
          isDirty: false,
        };
      } else {
        return {
          ...state,
        };
      }
    },
    invoicePage_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          invoicePageList: list,
          editId: '',
          selectionIds: [],
          selectedRows: [],
          isDirty: false,
        };
      } else {
        return {
          ...state,
        };
      }
    },
    // 处理发送
    audit_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('操作成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '操作失败！');
      }
      return {
        ...state,
        selectionIds: [],
        selectedRows: [],
      };
    },
    check_r(state, action) {
      const { selectedRows } = state;
      let orderAmount = 0.0;
      selectedRows.forEach(row => {
        orderAmount = orderAmount + row.feeSum;
      });
      let invoiceForm = {};
      const { payload } = action;
      if (payload && payload.code === 0) {
        invoiceForm.invoiceInfo = {
          name: 'invoiceInfo',
          value: payload.result,
        };
        invoiceForm.orderAmount = {
          name: 'orderAmount',
          value: orderAmount,
        };
        invoiceForm.isFull = {
          name: 'isFull',
          value: 'INVOICE',
        };
        return {
          ...state,
          invoiceForm,
          invoiceModal: true,
        };
      } else {
        message.error(payload.errorMsg);
        return {
          ...state,
        };
      }
    },
  },
};
